from time import sleep

import pytest

from common.commin.comm_driver import CommDriver
from common.commin.commin import allure_save_screenshot
from common.log.log import log
from config.env import Env
from data.randomly_generated_data import generate_random_product_name
from po.jinhua.ht.jinhua_ht_main.jinhua_ht_main_page import JHHTHomePage
from po.public_methods import yc_ht_api_login, yg_ht_api_login, yg_applets_wx_api_login, yc_applets_wx_api_login, \
    yg_ui_login_backstage, yc_ui_login_backstage, yg_add_sp, yg_rmv_sp, yk_ui_login_backstage, yk_ht_api_login, \
    yk_tt_ui_login
from po.yk.ht.logingPage import LoginPage


@pytest.fixture(scope="module")
def yg_api_applets_wx_login():
    """
    花王云购api接口——微信授权登录
    return: 返回token
    """
    token = yg_applets_wx_api_login()
    yield token


@pytest.fixture(scope="module")
def yc_api_applets_wx_login():
    """
    花王云创api接口——微信授权登录
    return: 返回token
    """
    token = yc_applets_wx_api_login()
    yield token


@pytest.fixture(scope="module")
def yg_api_ht_login():
    """
    花王云购管理台api登录
    return: 返回token
    """
    token = yg_ht_api_login()
    yield token


@pytest.fixture(scope="module")
def yc_api_ht_login():
    """
    花王云创管理台api登录
    return: 返回token
    """
    token = yc_ht_api_login()
    yield token


@pytest.fixture(scope='module')
def yk_api_ht_login():
    """
    花王云客管理台登录
    返回token
    """
    token = yk_ht_api_login()
    yield token


@pytest.fixture(scope="module")
def yg_login_jh_backstage():
    """
    花王云购后台ui登录
    """
    global driver_chrome
    driver_chrome = yg_ui_login_backstage(mode=1)  # 默认远程，1为本地启动浏览器
    yield driver_chrome
    driver_chrome.quit()


@pytest.fixture(scope="module")
def yc_login_jh_backstage():
    """
    花王云创后台ui登录
    """
    global driver_chrome
    driver_chrome = yc_ui_login_backstage(mode=0)  # 默认远程，1为本地启动浏览器
    yield driver_chrome
    driver_chrome.quit()


@pytest.fixture(scope="module")
def yk_login_jh_backstage():
    """
    登录云客商城后台
    """
    log.info('#################登录云客商城后台###################')
    global driver_chrome
    driver_chrome = yk_ui_login_backstage(mode=1)  # 默认远程，1为本地启动浏览器
    yield driver_chrome
    log.info('################花王云客退出浏览器操作################')
    driver_chrome.quit()

@pytest.fixture(scope="module")
def yk_test_login_jh_backstage():
    """
    登录云客商城后台
    """
    log.info('登录云客商城后台')
    global driver_chrome
    driver_chrome = yk_tt_ui_login(mode=1)  # 默认远程，1为本地启动浏览器
    yield driver_chrome
    driver_chrome.quit()

@pytest.fixture(scope="function")
def yg_add_item():
    """
    前置条件：添加商品 后置条件：删除商品
    """
    item_name = generate_random_product_name()
    yg_add_sp(item_name)
    yield item_name
    yg_rmv_sp(item_name)


@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    """
    pytest 失败后执行
    :param item: 测试用例
    :param call: 测试步骤
    :return:
    """
    out = yield
    result = out.get_result()
    log.info(f"测试报告:{result}")
    log.info(f"执行耗时:{call.duration}")
    # if result.failed and result.when == 'call':
    if result.failed:
        try:
            allure_save_screenshot(driver_chrome, '失败截图')
            sleep(1)
            if 'hwyc' in driver_chrome.current_url:
                JHHTHomePage(driver_chrome).open_menu('yc', '主页')
            elif 'hwyg' in driver_chrome.current_url:
                JHHTHomePage(driver_chrome).open_menu('yg', '主页')
            elif 'jky' in driver_chrome.current_url:
                # 待补充
                pass
        except NameError:
            log.error('失败截图，暂时不开放')


@pytest.fixture(scope='module',autouse=False)
def fix_Yk_login():
    log.info('花王云客UI登录管理台后台')
    mainpage=LoginPage().open_yk_loginpage().login_yk(Env.ADMIN_USER,Env.ADMIN_PASSWORD,Env.ADMIN_VerificationCode)
    yield mainpage
    log.info("花王云客UI自动化结束")
    CommDriver().get_driver().quit()


